home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / RESIZE.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  6KB  |  232 lines

  1. #include "pt.h"
  2. #include "memory.h"
  3. #include "malloc.h"
  4.  
  5. int pascal
  6. /* XTAG:getBox */
  7. getBox(inRow, inCol, outRow1, outCol1, outRow2, outCol2, boxMode)
  8.     /* boxMode=0 is resize box; =1 is stretch box; =2 to move box */
  9.     int inRow, inCol, *outRow1, *outCol1, *outRow2, *outCol2;
  10. {
  11.     extern unsigned char msgBuffer[];
  12.     extern struct SREGS segRegs;
  13.     extern unsigned char *screenChars;
  14.     extern unsigned char border3[];
  15.     extern union REGS rin, rout;
  16.     extern int debug;
  17.     extern unsigned char borderColor;
  18.     extern int scrRows, scrCols;
  19.     extern int evhead, evtail;
  20.     extern struct event events[];
  21.     extern int menuLine;
  22.     extern int cursorMouse;
  23.     extern int mousePresent;
  24.     extern unsigned char *userMessages[];
  25.  
  26.     int curRow, curCol, row1, col1, row2, col2;
  27.     int row2save, col2save;
  28.     int r1, c1, r2, c2, rmin, rmax, offset, ret;
  29.     int saveCursorMouse, twoClicks, startButtons;
  30.     unsigned char far *sBuffer;
  31.     unsigned int sizeOfBuffer;
  32.     unsigned char *lineBuffer;
  33.     int lineSize;
  34.  
  35.     /* get the present mouse button state */
  36.     if( mousePresent ) {
  37.         rin.x.ax = 3;
  38.         int86(51, &rin, &rout);
  39.         startButtons = rout.x.bx;
  40.         if( boxMode == 2 ) {
  41.             inRow = rout.x.dx>>3;
  42.             inCol = rout.x.cx>>3;
  43.         }
  44.     } else {
  45.         startButtons = 0x7;
  46.         if( boxMode == 2 )
  47.             getCPos(&inRow, &inCol);
  48.     }
  49.         
  50.     /* save the bottom line */
  51.     lineSize = scrCols<<1;
  52.     lineBuffer = malloc(lineSize);
  53.     if( lineBuffer != NULL )
  54.         memcpy(lineBuffer, screenChars + (scrRows-1)*(scrCols<<1),
  55.             lineSize);
  56.     ret = 0;
  57.     saveCursorMouse = cursorMouse;
  58.     cursorMouse = 1;
  59.     twoClicks = 0;
  60.  
  61.     switch( boxMode ) {
  62.     case 0:    /* resize the whole box */
  63.         msg("Press the left mouse button at one corner", 1);
  64.         if( downButtons(&row1, &col1) ) {
  65.             ret = 2;
  66.             goto cancelBox;
  67.         }
  68.         if( menuLine > 0 && row1 == 0 ) {
  69.             /* this is so a double click on the menu line */
  70.             /* will default to a full size window */
  71.             row1 = 1;
  72.             col1 = 0;
  73.         }
  74.         startButtons = 0x7;    /* ignore more buttons */
  75.         msg("Release at the opposite corner", 1);
  76.         goto notFixed;
  77.     case 1:    /* stretch the box from the corner */
  78.         msg("Release at the new corner", 1);
  79.         row1 = inRow;
  80.         col1 = inCol;
  81.     notFixed:
  82.         r1 = r2 = row2 = row1;
  83.         c1 = c2 = col2 = col1;
  84.         break;
  85.     case 2:    /* move the box from the corner */
  86.         msg("Release at the new position", 1);
  87.         r1 = row1 = *outRow1;
  88.         c1 = col1 = *outCol1;
  89.         r2 = row2save = *outRow2;
  90.         c2 = col2save = *outCol2;
  91.         row2 = inRow;
  92.         col2 = inCol;
  93.         break;
  94.     }
  95.  
  96.     /* save the present screen characters */
  97.     /* allocate some memory to hold the screen image */
  98.     sizeOfBuffer = (unsigned)((scrCols<<1)*scrRows);
  99.     sBuffer = _fmalloc(sizeOfBuffer);
  100.     if( sBuffer == 0L ) {
  101.         msg(userMessages[NOSPACEMSG], 3);
  102.         if( lineBuffer != NULL ) {
  103.             free(lineBuffer);
  104.         }
  105.         return 1;    /* getBox cancelled */
  106.     }
  107.     movedata(segRegs.ds, (int)screenChars, FP_SEG(sBuffer),
  108.         FP_OFF(sBuffer), sizeOfBuffer);
  109.  
  110.     /* this loops follows the mouse and redraws the shadow box */
  111.     while( 1 ) {
  112.         if( !isMouseEvent(1) )
  113.             continue;    /* wait for a mouse event */
  114.         evhead = getMouseEvent();
  115.  
  116.         /* skip intermediate mouse movements (no button changes) */
  117.         while( events[evhead].mask == 1 && evhead != evtail ) {
  118.             if( ++evhead >= NEVENTS )
  119.                 evhead = 0;
  120.         }
  121.  
  122.         /* check for more buttons to be than we at the beginning */
  123.         if( ((~startButtons) & events[evhead].buttons) != 0 ) {
  124.             ret = 1;
  125.             break;
  126.         }
  127.  
  128.         /* stop at a mouse click or release */
  129.         if( events[evhead].buttons == 0 ) {
  130.             /* twoClicks is for no-mouse operation */
  131.             if( mousePresent || twoClicks )
  132.                 break;
  133.         }
  134.         
  135.         /* set twoClicks on the second click */
  136.         if( !mousePresent && events[evhead].buttons != 0 )
  137.             twoClicks = 1;
  138.  
  139.         /* get the new corner */
  140.         curRow = events[evhead].vertical>>3;
  141.         /* check for going off the bottom of the screen */
  142.         /* This can happen if the mouse driver is not set right */
  143.         if( curRow >= scrRows )
  144.             curRow = scrRows - 1;
  145.         curCol = events[evhead].horizontal>>3;
  146.         /* check for going off the right of the screen */
  147.         /* This can happen if the mouse driver is not set right */
  148.         if( curRow >= scrCols )
  149.             curRow = scrCols - 1;
  150.  
  151.         /* has the mouse moved? */
  152.         if( curRow != row2 || curCol != col2 ) {
  153.  
  154.             /* erase the old box (except the first iteration) */
  155.             sizeOfBuffer = (unsigned)((scrCols<<1)*(r2-r1+1));
  156.             offset = (scrCols<<1)*r1;
  157.             movedata(FP_SEG(sBuffer), FP_OFF(sBuffer)+offset,
  158.                 segRegs.ds, (int)(screenChars+offset),
  159.                 sizeOfBuffer);
  160.  
  161.             /* determine the corners of the new shadow box */
  162.             if( boxMode == 2 ) {
  163.                 offset = curCol - inCol;
  164.                 c1 = col1 + offset;
  165.                 if( c1 < 0 )
  166.                     c1 = 0;
  167.                 c2 = col2save + offset;
  168.                 if( c2 > (scrCols-1) )
  169.                     c2 = (scrCols-1);
  170.                 offset = curRow - inRow;
  171.                 rmin = r1;
  172.                 r1 = row1 + offset;
  173.                 if( r1 < 0 )
  174.                     r1 = 0;
  175.                 if( r1 < rmin )
  176.                     rmin = r1;
  177.                 rmax = r2;
  178.                 r2 = row2save + offset;
  179.                 if( r2 > (scrRows-1) )
  180.                     r2 = (scrRows-1);
  181.                 if( r2 > rmax )
  182.                     rmax = r2;
  183.             } else {
  184.                 r1 = min(row1, curRow);
  185.                 c1 = min(col1, curCol);
  186.                 r2 = max(row1, curRow);
  187.                 c2 = max(col1, curCol);
  188.             }
  189.             if( menuLine > 0 && r1 == 0 )
  190.                 r1 = 1;
  191.             if( menuLine < 0 && r2 == (scrRows-1) )
  192.                 --r2;
  193.             /* highlight the border */
  194.             setMap(r1, c1, r2, c2, 1, 0x07);
  195.             drawBorder(&border3[0], r1, c1, r2, c2, borderColor,
  196.                 0);
  197.             if( boxMode != 2 ) {
  198.                 rmin = min(r1, min(r2, row2));
  199.                 rmax = max(r1, max(r2, row2));
  200.             }
  201.             updateScreen(rmin, rmax);
  202.             row2 = curRow;
  203.             col2 = curCol;
  204.         }
  205.     }
  206.  
  207.     /* restore the screen */
  208.     sizeOfBuffer = (unsigned)((scrCols<<1)*scrRows);
  209.     movedata(FP_SEG(sBuffer), FP_OFF(sBuffer), segRegs.ds,
  210.         (int)screenChars, sizeOfBuffer-(scrCols<<1));
  211.         /* don't restore the last row since we will rewrite */
  212.         /* in immediately (6 lines down) */
  213.     _ffree(sBuffer);
  214.  
  215. cancelBox:
  216.     /* retore the bottom line */
  217.     if( lineBuffer != NULL ) {
  218.         memcpy(screenChars+(scrRows-1)*(scrCols<<1), lineBuffer,
  219.             lineSize);
  220.         free(lineBuffer);
  221.     }
  222.     updateScreen(0, scrRows-1);
  223.     
  224.     /* return the new corners */
  225.     *outRow1 = r1;
  226.     *outCol1 = c1;
  227.     *outRow2 = r2;
  228.     *outCol2 = c2;
  229.     cursorMouse = saveCursorMouse;
  230.     return ret;
  231. }
  232.